use ATE 
Go




select * from PowerTest 
select * from PowerTestItem



IF OBJECT_ID ( 'PowerTestItem_Insert', 'P' ) IS NOT NULL 
    DROP PROCEDURE PowerTestItem_Insert;
GO
CREATE PROCEDURE PowerTestItem_Insert
	@ID int =0,
	@TestPort nvarchar(50) =null,
	@StartTime datetime,
	@Duration int =0,
	@FrePoint decimal(18,4) =0,
	@InputPower decimal(8,2) =0,
	@LowLimit decimal(8,2) =0,
	@Measurement decimal(8,2) =0,
	@IsPass bit =0
AS    
    SET NOCOUNT ON;
    if(@ID is not null)
    insert into PowerTestItem(ID,TestPort,StartTime,Duration,FrePoint,InputPower,LowLimit,Measurement,IsPass)
	Values(@ID,@TestPort,@StartTime,@Duration,@FrePoint,@InputPower,@LowLimit,@Measurement,@IsPass);	
GO
--


IF OBJECT_ID ( 'PowerTest_Insert', 'P' ) IS NOT NULL 
    DROP PROCEDURE PowerTest_Insert;
GO
CREATE PROCEDURE PowerTest_Insert
    @ID int output,
	@ProductSN nvarchar(50)=null,
	@ProductTypeID nvarchar(50)=null,
	@CouplerValue nvarchar(10)=null,
	@UserID nvarchar(10)=null,
	@TestStep nvarchar(10)=null,
	@PCIP nvarchar(50)=null,
	@SignalSource nvarchar(50)=null,	
	@IsPass bit=0
AS    
	begin	             
	if(Exists(select * from PackageTest where PackageProductSN=@ProductSN))
		begin 
		set @ProductSN=(select top 1 ProductSN from PackageTest where PackageProductSN=@ProductSN)
		insert into PowerTest(ProductSN,RecordTime,ProductTypeID,CouplerValue,UserID,TestStep,PCIP,SignalSource,IsPass)
		Values(@ProductSN,GETDATE(),@ProductTypeID,@CouplerValue,@UserID,@TestStep,@PCIP,@SignalSource,@IsPass);
		end		
	else 
		begin 
		if(@ProductSN is not null)
		insert into PowerTest(ProductSN,RecordTime,ProductTypeID,CouplerValue,UserID,TestStep,PCIP,SignalSource,IsPass)
		Values(@ProductSN,GETDATE(),@ProductTypeID,@CouplerValue,@UserID,@TestStep,@PCIP,@SignalSource,@IsPass);
		end	
	set @ID=@@IDENTITY; 
	end	
GO

















